//
//  PersonalController.m
//  PropertyMananger
//
//  Created by bh on 17/3/2.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "PersonalController.h"
#import "ModifyPwdCell.h"
#import "ModfiyComfimCell.h"
#import "AccountCell.h"
#import "AccountInputCell.h"
#import "PersonHeaderView.h"
#import "PersonalModel.h"

@interface PersonalController ()<UINavigationControllerDelegate>
@property (nonatomic,strong) NSArray * titleArray;
@property (nonatomic,strong) NSArray * placeTitleArray;
@property (nonatomic,strong) NSMutableArray * personArray;

@property (nonatomic,strong) PersonHeaderView * headerView;

@end

@implementation PersonalController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self initTitleViewName:@"个人信息"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

     self.personArray = [[NSMutableArray alloc]init];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH) isGroup:NO];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BHGrayColor;
    
    self.headerView = [[PersonHeaderView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 150)];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView.backBtn addTarget:self action:@selector(returnBtn:) forControlEvents:UIControlEventTouchUpInside];
    
     self.navigationController.delegate = self;
    
    
    self.titleArray = @[@"账号",@"工号",@"姓名",@"部门",@"职位",@"联系电话"];
  //  self.placeTitleArray = @[@"请输入昵称",@"请输入姓名",@"请输入部门",@"请输入职位",@"请输入联系电话"];
    
    [self postMyInfomationData];
}
- (void)returnBtn:(UIButton *)button{

    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return 6;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        
        AccountCell * cell = [AccountCell cellWithTableView:tableView];
        cell.titleLabel.text = self.titleArray[indexPath.row];

    if(self.personArray.count != 0){
        PersonalModel * model = self.personArray[0];
        if (indexPath.row == 0) {
    
            cell.accountLabel.text = model.LoginName;
        }else if (indexPath.row == 1){
    
        cell.accountLabel.text = model.WorkerCode;
    
        }else if (indexPath.row == 2){
    
            cell.accountLabel.text = model.RealName;
    
        }else if (indexPath.row == 3){
    
            cell.accountLabel.text = model.DeptName;
    
        }else if (indexPath.row == 4){
    
            cell.accountLabel.text = model.Duty;
            
        }else if (indexPath.row == 5){
            
            cell.accountLabel.text = model.Tel;
            
        }
        
    }
          return cell;
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return 50;
    
}

#pragma mark - 数据请求
- (void)postMyInfomationData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    NSDictionary * parms = nil;
        parms =  @{
                   @"WorkerID":userModel.WorkerID,
                   @"json":@"\{\"jsonEntity\":\{},\"action\":\"getmyinfo\",\"version\":\"V1.0\"}"
                   };
    
    DLog(@"提交的参数 === %@",parms);
    
    [MineRequest postMyInfomationSuccess:^(id data) {
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * personDic = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"返回 === %@",dict[@"Code"]);
        DLog(@"返回提示 === %@",dict[@"Msg"]);
        DLog(@"我的个人信息数据 == %@",personDic);
        
        PersonalModel * model = [[PersonalModel alloc]init];
        [model setValuesForKeysWithDictionary:personDic];
        
        [self.personArray addObject:model];
    
        PersonalModel * model2 = self.personArray[0];
        DLog(@"%@",model2.WorkerCode);
        [MBProgressHUD hideHUDForView:self.view];
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
        [MBProgressHUD hideHUDForView:self.view];

        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
